
package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.yjnh.bean.EventBusConfig;
import com.hemaapp.yjnh.bean.EventBusMsg;
import com.hemaapp.yjnh.bean.SysInitInfo;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;

import de.greenrobot.event.EventBus;
import xtom.frame.util.XtomToastUtil;

/**
 * 银行卡申请提现页面
 */
public class Cash2BankActivity extends BaseActivity {
	
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private View bankView;
	private TextView bankname;
	private TextView bankuser;
	private TextView bankcard;
	private TextView feeaccount;
	private EditText applyfee;
	private EditText password;
	private Button button;
	private TextView tv_user_intro;

	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_cashadd);
		super.onCreate(savedInstanceState);
		user = getApplicationContext().getUser();
	}

	@Override
	protected void onResume() {
		initInfo();
		super.onResume();
	}

	private void initInfo() {
		if (user != null) {
			bankname.setText(user.getBankname());
			bankuser.setText(user.getBankuser());
			bankcard.setText(user.getBankcard());
			feeaccount.setText(user.getFeeaccount());
		}
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_ADD:
			showProgressDialog("正在提交申请");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_ADD:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_ADD:
			XtomToastUtil.showShortToast(mContext, "申请提交成功");
			EventBusMsg msg = new EventBusMsg();
			msg.setType(EventBusConfig.UPDATE_INFO);
			EventBus.getDefault().post(msg);
			titleText.postDelayed(new Runnable() {

				@Override
				public void run() {
					finish();
				}
			}, 1000);
			break;
		default:
			break;
		}

	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_ADD:
			XtomToastUtil.showShortToast(mContext, "申请提交失败, "+baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CASH_ADD:
			XtomToastUtil.showShortToast(mContext, "申请提交失败, 请稍后再试");
			break;
		default:
			break;
		}
	}

	@Override
	protected void findView() {
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleText = (TextView) findViewById(R.id.title_text);
        titleRight = (Button) findViewById(R.id.title_right_btn);

		bankView = findViewById(R.id.bank);
		bankname = (TextView) findViewById(R.id.bankname);
		bankuser = (TextView) findViewById(R.id.bankuser);
		bankcard = (TextView) findViewById(R.id.bankcard);
		feeaccount = (TextView) findViewById(R.id.feeaccount);
		applyfee = (EditText) findViewById(R.id.applyfee);
		password = (EditText) findViewById(R.id.password);
		button = (Button) findViewById(R.id.button);
		tv_user_intro = (TextView) findViewById(R.id.tv_user_intro);
	}

	@Override
	protected void getExras() {

	}

	@Override
	protected void setListener() {
		titleText.setText("银行卡提现");
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.GONE);
		bankView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent(mContext, BankEditActivity.class);
				startActivity(it);
			}
		});
		button.setOnClickListener(new CommitListener());
		SysInitInfo info = getApplicationContext().getSysInitInfo();
		tv_user_intro.setText("初始密码：123456 \n如忘记密码请致电客服："+info.getSys_service_phone());
	}

	private class CommitListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String number = applyfee.getText().toString();
			if (isNull(number)) {
				showTextDialog("请输入提现金额");
				return;
			}
			int pn = 0;
			try {
				pn = Integer.parseInt(number);
			} catch (Exception e) {
				//
			}
			if (pn <= 0 || (pn % 100) != 0) {
				showTextDialog("提现金额必须为100的整数倍哦");
				return;
			}
			String pw = password.getText().toString();
			if (isNull(pw)) {
				showTextDialog("请输入密码");
				return;
			}

			String token = getApplicationContext().getUser().getToken();
			getNetWorker().cashAdd(token,"1", number, pw);

		}
	}

}
